package com.ambow.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.Entity.Bclass;
import com.ambow.Entity.Category;
import com.ambow.Entity.Level;
import com.ambow.Entity.Major;
import com.ambow.Entity.Operator;
import com.ambow.Entity.PackageBook;
import com.ambow.Entity.Student;
import com.ambow.Entity.TextBook;
import com.ambow.service.PackageService;
import com.ambow.service.TextBookService;
import com.ambow.service.impl.PackageServiceImpl;
import com.ambow.service.impl.TextBookServiceImpl;
import com.ambow.utils.BaseDAO;

/**
 * Servlet implementation class PackageBookServlet
 */
@WebServlet("/PackageBookServlet")
public class PackageBookServlet extends HttpServlet {
	TextBookService tBookService = new TextBookServiceImpl();
	PackageService packageService = new PackageServiceImpl();    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String status = request.getParameter("status");
		if (status.equals("queryBook")){
			queryBook(request, response);// 查询要加入到套书中的教材
		} else if (status.equals("addPackageBook")){
			addPackageBook(request,response);// 增加套书
		} else if (status.equals("queryPackageBook")) {
			queryPackageBook(request,response);// 查询套书
		} else if (status.equals("queryStudent")) {
			queryStudent(request,response);// 查询学生
		}else if(status.equals("showBook")) {//显示要加入到套书中的教材
			showBook(request,response);
		}
		
	}
	private void showBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 显示要加入到套书中的教材
		Integer tid=Integer.valueOf(request.getParameter("radio"));
		String hql="from TextBook where id="+tid;
		TextBook textBook=tBookService.queryBook(hql);
		if(textBook!=null) {
			request.setAttribute("textBook",textBook);
			request.getRequestDispatcher("pages/module/storage/storage001/student/edit_package.jsp").forward(request, response);
		}
	}

	private void queryStudent(HttpServletRequest request, HttpServletResponse response) {
		//查询学生
		try {

			Integer classid= Integer.valueOf(request.getParameter("bclass"));// 班级名称
			System.out.println(classid);
			String number = request.getParameter("number");// 学号
			Integer packageBook_id = Integer.valueOf(request.getParameter("packageBook_id"));// 套书id
			System.out.println("jjjjjjjjjjjjjj'" + packageBook_id);
			String hql = "from Student where number ='" + number + "' and bclass_id=" + classid;
			List<Student> students =packageService.queryAllStudent(hql);

			System.out.println(students.size() + "kkkkkkkkkkkkkk'");
			if (students != null && !students.isEmpty()) {
				PackageBook packageBook = packageService.queryById(PackageBook.class, packageBook_id);
				System.out.println(packageBook);
				List<PackageBook> packageBooks = new ArrayList<PackageBook>();
				packageBooks.add(packageBook);
				System.out.println(packageBooks);
				System.out.println("hahahhaaaaaaaa");
				request.setAttribute("students", students);
				request.setAttribute("packageBooks", packageBooks);
				request.getRequestDispatcher("pages/module/storage/storage001/student/fetch.jsp").forward(request,
						response);
			} else {
				PackageBook packageBook = packageService.queryById(PackageBook.class, packageBook_id);
				List<PackageBook> packageBooks = new ArrayList<PackageBook>();
				packageBooks.add(packageBook);
				request.setAttribute("packageBooks", packageBooks);
				request.getRequestDispatcher("pages/module/storage/storage001/student/student.jsp").forward(request,
						response);
			}

		} catch (Exception e) {
		}
	}

	private void queryPackageBook(HttpServletRequest request, HttpServletResponse response) {
		// 查询套书
		try {

			String acedemicYear=request.getParameter("acedemicYear");//学年
			System.out.println(acedemicYear);
			String semester=request.getParameter("semester");//学期
			System.out.println(semester);
			String grade= request.getParameter("grade");// 年级
			System.out.println(grade);
			Integer cid=Integer.valueOf(request.getParameter("categoryName"));// 类别id
			System.out.println(cid);
			Integer mid=Integer.valueOf(request.getParameter("majorName"));// 专业id	
			System.out.println(mid);
			Integer lid=Integer.valueOf(request.getParameter("levelName"));// 层次id
			System.out.println(lid);
			String hql ="from PackageBook where acedemicYear=? and semester=? and grade=? and category_id="+cid
					+"and major_id="+mid+"and level_id="+lid;
			String[] param ={acedemicYear,semester,grade};
			System.out.println("123456saddfgt");
			List<PackageBook> packageBooks = packageService.queryPackageBook(hql, param);
			System.out.println(packageBooks+"2134567899");
			if (packageBooks!= null&&!packageBooks.isEmpty()) {
				System.out.println("查询套书成功");
				request.setAttribute("packageBooks", packageBooks);
				request.getRequestDispatcher("pages/module/storage/storage001/student/student.jsp").forward(request,response);
			} else {
				System.out.println("套书查询失败");
				response.sendRedirect("pages/module/storage/storage001/student/query_package.jsp");
			}
		} catch (Exception e) {
		}
	}

	private void addPackageBook(HttpServletRequest request, HttpServletResponse response) {
		//编辑套书
		System.out.println("wijughdndsfjjskdjfiojdgnjnjn");
		try {
			String acedemicYear=request.getParameter("acedemicYear");//学年
			String semester=request.getParameter("semester");//学期
			Integer cid=Integer.valueOf(request.getParameter("categoryName"));// 类别id
			Category category=packageService.queryCategoryById(cid);
			System.out.println(cid);
			Integer mid=Integer.valueOf(request.getParameter("majorName"));// 专业id
			Major major=packageService.queryMajorById(mid);		
			System.out.println(mid);
			Integer lid=Integer.valueOf(request.getParameter("levelName"));// 层次id
			Level level=packageService.queryLevelById(lid);
			System.out.println(lid);
			Integer tid=Integer.valueOf(request.getParameter("radio"));//教材id
			TextBook tbook=tBookService.queryOne(TextBook.class, tid);					
			String grade=request.getParameter("grade");//年级
			String pdescription=request.getParameter("pdescription");//描述		
			PackageBook pBook=new PackageBook();
			pBook.setAcedemicYear(acedemicYear);
			pBook.setCategory(category);
			pBook.setGrade(grade);
			pBook.setMajor(major);
			pBook.setLevel(level);
			pBook.setTextBook(tbook);
			pBook.setSemester(semester);
			if(request.getSession().getAttribute("op")!=null) {
				System.out.println("123456778");
				pBook.setOperator((Operator)request.getSession().getAttribute("op"));
			}else {
				System.out.println("12345678dfgggggg");
				pBook.setOperator((Operator)BaseDAO.get(Operator.class, 1));
			}
			pBook.setPdescription(pdescription);
			boolean b=packageService.addPackageBook(pBook);
			if(b) {
				System.out.println("套书添加成功");
				String hql = "from PackageBook";
				List<PackageBook> packageBooks = packageService.queryAll(hql);
				request.setAttribute("packageBooks", packageBooks);
				request.getRequestDispatcher("pages/module/storage/storage001/student/query_package.jsp").forward(request, response);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		
		
	}

	private void queryBook(HttpServletRequest request, HttpServletResponse response) {
		// 查询要加入到套书中的书籍
		try {
			String bookName = request.getParameter("bookName");// 教材名
			String author = request.getParameter("author");// 作者
			String publish = request.getParameter("publish");// 出版社 
			List<TextBook> tBooks = new ArrayList<TextBook>();
			if (bookName.length()!=0 && author.length()!=0 && publish.length()!=0) {
				System.out.println("23456sddfggffff");
				String hql ="from TextBook where name=? and author=? and publish=?";
				String[] param = { bookName, author, publish};
				TextBook textBook = tBookService.queryByHql(hql,param);// 按教材名、作者、出版社查询
				tBooks.add(textBook);
			} else {
				String hql = "from TextBook";
				tBooks = tBookService.queryAllBook(hql);
			}
			request.setAttribute("tBooks", tBooks);
			request.getRequestDispatcher("pages/module/storage/storage001/student/query_book.jsp").forward(request,
					response);
		} catch (Exception e) {
		}
	}
}
